﻿@{
    ViewBag.Title = "接口管理";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<style type="text/css">
    .layout-split-west {
        border-right-color: Window;
    }
</style>

<div class="easyui-layout" data-options="fit:true" scroll="no">
    <div data-options="region:'west',split:true,title:'机构',iconCls: 'icon-org'" style="width:210px">
        <ul id="tgOrg"></ul>
    </div>
    <div data-options="region:'center',noheader:true,border:false">
        <table id="dgApiAccount"></table>
    </div>
</div>
<div id="tbar">
    <div class="tbar">
        <div class="z-toolbar">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow_refresh',plain:true" onclick="ToolbarRefresh();">刷新</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="ToolbarCreate();">添加</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="ToolbarEdit();">编辑</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cross',plain:true" onclick="ToolbarRemove();">删除</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="ToolbarUnselect();">取消选中</a>
        </div>
    </div>
</div>
@section PageSpecificScript{
    <script type="text/javascript">
        var dgApiAccount;/*显示用户*/
        var ctypeTree;
        var cpStatusData;
        $(document).ready(function () {
            $.validateUrl("/Admin/UO_Organization/GetTreeOrgInfo", function () {
                deptTree = $("#tgOrg").tree({
                    url: '/Admin/UO_Organization/GetTreeOrgInfo',
                    onLoadSuccess: function (node, data) {
                        if (data.Msg) {
                            xz.dealWith(data);
                        }
                    },
                    onClick: function (node) {
                        dgApiAccount.datagrid('load', {
                            OrgId: node.id
                        });
                    },
                    onDblClick: function (node) {
                        if (node.state = "closed") {
                            $(this).tree("expand", node.target);
                        } else {
                            $(this).tree("collapse", node.target);
                        }
                    }
                });
                loadGrid();
            });
        }, xz.dealWith, 'GET');
        /*加载datagrid*/
        function loadGrid() {
            dgApiAccount = $("#dgApiAccount").datagrid({
                url: '/Admin/Sys_ApiAccount/GetApiAccountByCondition',
                method: 'Post',
                idField: 'ApiId',
                fit: true,
                fitColumns: true,
                rownumbers: true,
                striped: true,
                pagination: true,
                singleSelect: true,
                toolbar: '#tbar',
                pageSize: 20,
                pageList: [20, 30, 40, 50],
                frozenColumns: [[
                    { field: 'ApiId', title: 'ApiId', hidden: true }
                ]],
                columns: [[
                    {
                        field: 'ApiType', title: '接口类别', width: 60, halign: 'center', align: 'center', formatter: function (value, row, index) {
                            if (value == "tmall") {
                                return "天猫接口";
                            } else if (value == "pingan") {
                                return "平安接口";
                            } else if (value == "guoshou") {
                                return "国寿接口";
                            } else if (value == "app") {
                                return "APP接口";
                            } else {
                                return "未知";
                            }
                        }
                    },
                    { field: 'ApiUrl', title: '接口地址', width: 90, halign: 'center', align: 'center' },
                    { field: 'ApiName', title: '接口账号', width: 60, halign: 'center', align: 'center' },
                    { field: 'ApiPwd', title: '接口密码', width: 60, halign: 'center', align: 'center' },
                    {
                        field: 'ApiCreateTime', title: '创建日期', width: 70, halign: 'center', align: 'center', formatter: function (value, row, index) {
                            if (value) {
                                return eval("new " + value.substr(1, value.length - 2)).toLocaleDateString();
                            }
                        }
                    },
                    { field: 'ApiRemark', title: '说明', width: 100, halign: 'center', align: 'center' }
                ]]
            });
        }
        /*过滤用户信息*/
        function btnSearch_OnClick() {
            dgApiAccount.datagrid('load', xz.serializeObject(searchForm));
        }
        /**刷新**/
        function ToolbarRefresh() {
            dgApiAccount.datagrid("load");
        }
        /**增加接口**/
        function ToolbarCreate() {
            showAppendView();
        }
        /**编辑接口**/
        function ToolbarEdit() {
            showEditView();
        }
        /**删除菜单**/
        function ToolbarRemove() {
            removeApiAccount();
        }
        /**取消选中菜单**/
        function ToolbarUnselect() {
            dgApiAccount.datagrid("unselectAll");
        }
        /*打开增加接口的页面*/
        function showAppendView() {
            $.validateUrl('/Admin/Sys_ApiAccount/Edit', appendApiAccount, xz.dealWith, 'GET');
        }
        /*新增*/
        function appendApiAccount() {
            var pAdd = xz.dialog({
                title: '新增接口',
                width: 680,
                height: 300,
                iconCls: 'icon-add',
                href: '/Admin/Sys_ApiAccount/Edit',
                buttons: [{
                    text: '生成',
                    iconCls: 'icon-add',
                    handler: function () {
                        var f = pAdd.find('form');
                        f.form('submit', {
                            url: '/Admin/Sys_ApiAccount/CreateApiAccount',
                            onSubmit: function () {
                                return true;
                            },
                            success: function (jsonData) {
                                jsonData = $.parseJSON(jsonData);
                                $.procAjaxMsg(jsonData, function () {
                                    pAdd.dialog('close');
                                    dgApiAccount.datagrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        pAdd.dialog('close');
                    }
                }],
                onLoad: function () {
                    var f = pAdd.find('form');
                    var apiType = f.find('input[name=ApiType]');
                    var apiOrg = f.find('input[name=ApiOrg]');

                    window.setTimeout(function () {
                        apiType.focus();
                    }, 100);

                    apiType.combobox({
                        url: '/Admin/Sys_Dict/GetDictValueByKey?keyId=1',
                        method: 'GET',
                        valueField: 'text',
                        textField:'text',
                        required: true
                    });

                    apiOrg.combotree({
                        url: '/Admin/UO_Organization/GetTreeOrgInfo',
                        method: 'GET',
                        required: true,
                        lines: true,
                        multiple: true,
                        cascadeCheck: false,
                        valueField: 'id',
                        textField: 'text',
                        editable: false
                    });

                }
            })
        }
        /*编辑*/
        function showEditView() {
            $.validateUrl('/Admin/Sys_ApiAccount/Edit', edit, xz.dealWith, 'GET');
        }
        /*编辑*/
        function edit() {
            var node = dgApiAccount.datagrid("getSelected");
            if (node) {
                var pAdd = xz.dialog({
                    title: '修改码号',
                    width: 680,
                    height: 350,
                    iconCls: 'icon-edit',
                    href: '/Admin/Sys_ApiAccount/Edit',
                    buttons: [{
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            var f = pAdd.find('form');
                            f.form('submit',{
                                url: '/Admin/Sys_ApiAccount/EditApiAccount',
                                success: function (jsonData) {
                                    jsonData = $.parseJSON(jsonData);
                                    $.procAjaxMsg(jsonData, function () {
                                        pAdd.dialog('close');
                                        dgApiAccount.datagrid("reload");
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    }, function () {
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    });
                                }
                            });
                        }
                    }, {
                        text: '关闭',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            pAdd.dialog('close');
                        }
                    }],
                    onLoad: function () {
                        var f = pAdd.find('form');
                        var apiType = f.find('input[name=ApiType]');
                        var apiOrg = f.find('input[name=ApiOrg]');

                        window.setTimeout(function () {
                            apiType.focus();
                        }, 100);

                        apiType.combobox({
                            url: '/Admin/Sys_Dict/GetDictValueByKey?keyId=1',
                            method: 'GET',
                            valueField: 'text',
                            textField: 'text',
                            required: true
                        });

                        apiOrg.combotree({
                            url: '/Admin/UO_Organization/GetTreeOrgInfo',
                            method: 'GET',
                            required: true,
                            lines: true,
                            multiple: true,
                            cascadeCheck: false,
                            valueField: 'id',
                            textField: 'text',
                            editable: false
                        });

                        f.form('load', node);
                    }
                })
            } else {
                xz.messageAlert("提示", "请选择要修改的码号", "error");
            }
        }

        /*删除*/
        function removeApiAccount() {
            var rows = dgApiAccount.datagrid("getSelected");
            if (rows) {
                xz.messageConfirm('询问', '你确定要删除【' + rows.ApiUrl + '】吗?', function (b) {
                    if (b) {
                        $.ajax({
                            type: 'POST',
                            url: '/Admin/Sys_ApiAccount/DelApiAccount',
                            data: {
                                ApiId: rows.ApiId
                            },
                            cache: false,
                            dataType: 'JSON',
                            success: function (jsonData) {
                                $.procAjaxMsg(jsonData, function () {
                                    dgApiAccount.datagrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                });
            } else {
                xz.messageAlert("提示", "请选择要删除的码", "error");
            }
        }


        //选择上级节点
        function selectParent(pid) {
            $("[name=OrgID]:checkbox").each(function () {
                if (pid > 0) {
                    var checked = $(this).attr("checked");
                    $("[name=OrgID]:checkbox").each(function () {
                        if (pid == $(this).val() && checked == "checked") {
                            $(this).attr("checked", "checked");
                            selectParent($(this).parent().parent().parent().siblings().eq(0).find("div").text());
                            return false;
                        }
                    })
                }
            })
        }
        //选择下级节点
        function selectChildren(modID, checked) {
            $("[name=OrgID]:checkbox").each(function () {
                var parentID = $(this).parent().parent().parent().siblings().eq(0).find("div").text();
                if (modID == parentID) {
                    if (checked != "checked") {
                        $(this).removeAttr("checked");
                    }
                    selectChildren($(this).val(), $(this).attr("checked"));
                }
            })
        }
    </script>
}
